<template>
	<view>
		<view class="margin-lr">
			<view class="cu-list menu">
				<view class="cu-item flex"
					v-for="(recordListVal, recordListIdx) in recordList"
					:key="recordListIdx">
					<view class="content">
						<view class="justify-between">
							<view class="flex">
								<text class="cuIcon-title text-olive"></text>
								<navigator :url="'./report_record_detail?record_id='+recordListVal._id">{{recordListVal.person_name}}</navigator>
							</view>
							<view>{{new Date(recordListVal.create_time).Format("MM-dd HH:mm")}}</view>
							<!-- <button class="cu-btn margin-right" @click="rewriteRecord(recordListVal._id)">重填</button> -->
							<button class="cu-btn margin-right" @click="deleteRecord(recordListVal._id,recordListIdx)">删除</button>
						</view>
						<view>
							<text class="text-grey text-sm">{{syntaxJson(recordListVal.model)}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import '@/common/js/date.js';

	export default {
		data() {
			return {
				report_id: "",
				recordList: [],
				page: 0,
			}
		},
		onLoad() {
			this.report_id = this.$route.query.report_id;
			if (this.report_id) {
				this.loadRecord();
			}
		},
		onPullDownRefresh() {
			if (this.report_id) {
				this.loadRecord();
			}
		},
		onReachBottom() {
			if (this.report_id) {
				this.loadRecord();
			}
		},
		methods: {
			loadRecord() {
				uni.showLoading({
					title: "加载中..."
				});
				let token = uni.getStorageSync("token");

				if (token) {
					uniCloud.callFunction({
						name: "ha_get2_record",
						data: {
							token,
							type: 0,
							report_id: this.report_id,
							page: this.page,
							length: 10
						},
						success: ({result}) => {
							// console.log(result)
							if ( result.status === 0 ) {
								this.page++;
								this.recordList.push(...result.data);
							} else {
								uni.showModal({
									content: `${result.msg || "网络异常，请稍后重试！"}`,
									showCancel: false,
									complete: function () {
										uni.navigateTo({url: "/pages/user/user_login"});
									}
								});
							}
						},
						fail() {
							uni.showToast({
								icon: 'none',
								title: '网络异常，请稍后重试！'
							});
						},
						complete() {
							uni.hideLoading();
							uni.stopPullDownRefresh();
						}
					});
				} else {
					uni.navigateTo({url: "/pages/user/user_login"});
				}
			},
			deleteRecord(record_id,index) {
				uni.showModal({ content: '确认删除吗？', success: (res) => { if (res.confirm) {
					let token = uni.getStorageSync("token");
					let report_id = this.report_id;

					if (token) {
						uniCloud.callFunction({
							name: "ha_del_report_record",
							data: {
								token,
								report_id,
								record_id
							},
							success: ({result}) => {
								// console.log(result);
								if ( result.status === 0 ) {
									uni.showToast({
										title: `${result.msg || "删除成功！"}`,
										duration: 1000
									});
									this.recordList.splice(index, 1);
								} else {
									uni.showModal({
										content: `${result.msg || "网络异常，请稍后重试！"}`,
										showCancel: false,
										complete: function () {
											uni.navigateTo({url: "/pages/user/user_login"});
										}
									});
								}
							},
							fail() {
								uni.showToast({
									icon: 'none',
									title: '网络异常，请稍后重试！'
								});
							}
						});
					} else {
						uni.navigateTo({url: "/pages/user/user_login"});
					}
				}}});
			},
			syntaxJson(json) {
				if (typeof json !== "string") {
					json = JSON.stringify(json);
				}
				return json.replace(/\{|}/g,'').replace(/\[|]/g,'').replace(/true/,'是').replace(/false/,'否').substr(0,50);
			}
		}
	}
</script>

<style>

</style>
